<?php

class My_Link {

    function  __construct($id = null) {

        $this->tableName = 'links';
        $this->table = new Zend_Db_Table($this->tableName);

        $info = $this->table->info();
        $this->primary = $info['primary'][1];

        $this->request = new Zend_Controller_Request_Http;

        if(is_array($id)){
            $id = $id[$this->primary];
        }
        if(!empty($id)){
            $this->setId($id);
            $this->readById();
        }
    }

    function setId($id){
        $this->id = $id;
    }

    function getId(){
        return $this->id;
    }

    function setStart($start){
        $this->data['start'] = $start;
    }

    function setEnd($end){
        $this->data['end'] = $end;
    }


    function setData($data = null){
        if(is_array($data)){
            $this->data['start']  = $data['start'];
            $this->data['end']    = $data['end'];
            $this->data['weight'] = $data['weight'];
        }
    }

    function getData($key = false){
        if($key === false){
            return $this->data;
        }
        return $this->data[$key];
    }

    function exists(){
        //if(is_array($this->data[0]))

        return $this->data[0][$this->primary] ? true : false;
    }

    function readById($id = null){
        if(!empty($id)){
            $this->setId($id);
        }
        if($this->getId() > 0){
            $result = $this->table->find($this->getId());
            $data = $result->toArray();
            $this->data = $data[0];
            return true;
        }
        return false;
    }

    function getAll(){
        $result = $this->table->fetchAll(
            $this->table->select()
                ->order('id')
            );
        return $result->toArray();
    }

    function getStart($id = null){
        if(!empty($id)){
            $this->setId($id);
        }
        
        if(!$this->exists()){
            $this->readById();
        }
        return $this->data['start'];
    }

    function getEnd($id = null){
        if(!empty($id)){
            $this->setId($id);
        }

        if(!$this->exists()){
            $this->readById();
        }
        return $this->data['end'];
    }

    function getWeight($id = null){
        if(!empty($id)){
            $this->setId($id);
        }

        if(!$this->exists()){
            $this->readById();
        }
        return $this->data['weight'];
    }

    function readByStart($start = null, $one = false){
        if(!empty($start)){
            $this->setStart($start);
        }elseif($start == 0){
            return false;
        }
        $start = $this->getStart();
        if(empty($start)){
            return false;
        }

        $table = new Default_Model_DbTable_Link();
        $query = $table->select()
                ->where('start = ?', $this->getStart())
                ->order('weight DESC');

        $rows = $table->fetchAll($query);
        $data = $rows->toArray();
        if(!$one){
            return $this->data = $data;
        }else{
            return $this->data = $data[0];
        }
    }

    function readByEnd($end = null){
        if(!empty($end)){
            $this->setEnd($end);
        }

        $table = new Default_Model_DbTable_Link();
        $rows = $table->fetchAll(
            $table->select()
                ->where('end = ?', $this->getEnd())
                ->order('weight ASC')
            );
        $this->data = $rows->toArray();
        return $this->data;
    }

    function insert($data = null){
        if(is_array($data)){
            $this->setData($data);
        }

        $id = $this->table->insert($this->getData());
        $this->setId($id);
        return $id;
    }

     /**
     * delete one row
     *
     * @param <type> $data
     * @return int number of rows deleted
     */
    function delete($data = null){
        if(is_array($data)){
            $this->setData($data);
        }
        if($this->exists()){
            $where = $this->table->getAdapter()->quoteInto($this->primary.' = ?', $this->getId());
            return $this->table->delete($where);
        }
        return false;
    }
}
?>
